<template>
    <el-container>
    <el-header>
        <span>电商后台管理系统</span>
        <el-button type="info" @click="checkout">退出</el-button>
    </el-header>
    <el-container>
        <el-aside width="200px">
            <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened router :default-active="activePath">  
                <el-submenu v-for="item in menuList" :key="item.id" :index="item.id+''">
                    <template slot="title">
                    <i class="el-icon-user"></i>
                    <span>{{item.authName}}</span>
                    </template>
                    <el-menu-item-group>
                        <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
                         <i class="el-icon-menu"></i>{{subItem.authName}}
                        </el-menu-item>
                    </el-menu-item-group>
                </el-submenu>
            </el-menu>
        </el-aside>
        <el-main>
            <router-view></router-view>
        </el-main>
    </el-container>
    </el-container>
</template>
<script>
export default ({ 
    data() {
        return {
            menuList: [],
            /* 保存激活菜单的path 控制高亮 */
            activePath: ''
        }
    },
    created() {
        this.getMenuList()
        /* 为了刷新后，还保持当前菜单的激活状态  刷新以后data中的activePath就会重置为''
          就会恢复没有高亮的状态，Vue重新实例化，要保持高亮状态，就要在数据挂载以前将高亮数据拿出来
          给activePath
         */
        this.activePath = window.sessionStorage.getItem('activePath') 
    },
    methods: {
        checkout: function() {
            window.sessionStorage.removeItem('token')
            this.$router.push('/login')
        },
        /* 发送请求获取菜单栏信息 */
        async getMenuList() {
            const datas = await this.$http.get('menus')
            const res = datas.data
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.menuList = res.data
        },
        /* 控制被激活菜单的高亮 */
        saveNavState(activePath) {
            window.sessionStorage.setItem('activePath', activePath) 
            this.activePath = activePath
        }
    }
 })
</script>
<style scoped>
    .el-header{
        background-color:#373d41;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .el-aside{
        background-color: #333744;
    }
    .el-main{
        background-color: #eaedf1;
    }
    .el-container{
        height:100%;
    }
</style>
